/**
 * @Date 14-11-2013
 * Main controller of the web application. used to get service and website details. Handles the requests matching url /listDetails 
 */
package com.globalblue;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

import com.globalblue.services.ServiceHandler;
import com.globalblue.websites.WebsiteHandler;

@WebServlet("/listDetails")
public class MainController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(MainController.class);

	/**
	 * doGet method of the servlet
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			logger.info("Main Controller");
			ServiceHandler sh = new ServiceHandler();
			req.getServletContext().setAttribute("servicesList", sh.getAllServices());

			WebsiteHandler wh = new WebsiteHandler();
			req.getServletContext().setAttribute("websiteList", wh.getAllWebsites());

			req.getRequestDispatcher("/MainPage").forward(req, resp);
			return;
		} catch (Exception e) {
			logger.log(Level.ERROR, e);
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}
}
